package com.blogs.ui;

import java.io.File;
import java.io.FileOutputStream;

import com.blogs.component.BaseActivity;
import com.blogs.component.MySurfaceView;
import com.blogs.entity.AppConfig;
import com.blogs.tools.ImageDownLoad;
import com.blogs.tools.ImageDownLoad.ImageCallBack;
import com.cnblogs.R;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ImageShowActivity extends BaseActivity implements OnClickListener {
	private String imageurl;
	// private ImageView iv_gif;
	private MySurfaceView iv_gif;
	private ProgressBar pb;
	private ImageView img_down_fail_iv;

	private ImageView zoom_out;
	private ImageView zoom_in;
	private ImageView image_save;
	private LinearLayout button_layout;

	private Bitmap bitmap;
	// private Matrix matrix = new Matrix();

	private Thread thread = null;

	private long lastTime = 0;
	private long mTime = 0;
	private boolean threadIsRuning = true;
	private boolean flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imageviewer);

		zoom_out = (ImageView) findViewById(R.id.zoom_out);
		zoom_in = (ImageView) findViewById(R.id.zoom_in);
		image_save = (ImageView) findViewById(R.id.image_save);
		button_layout = (LinearLayout) findViewById(R.id.button_layout);

		iv_gif = (MySurfaceView) findViewById(R.id.iv_gif);
		pb = (ProgressBar) findViewById(R.id.imagepsb);
		imageurl = getIntent().getStringExtra("imageurl");
		img_down_fail_iv=(ImageView)findViewById(R.id.img_down_fail_iv);

		zoom_out.setOnClickListener(this);
		zoom_in.setOnClickListener(this);
		image_save.setOnClickListener(this);
		showLoading(true);
		ImageDownLoad.loadDrawable(imageurl, new ImageCallBack() {

			public void onSuccess() {
				// TODO Auto-generated method stub

			}

			public void onSuccess(Bitmap Ibitmap) {
				// TODO Auto-generated method stub
				showLoading(false);
				if(Ibitmap==null)
				{
//					finish();
					showImgFaile(true);
				}
				else
				{
					bitmap = Ibitmap;
					iv_gif.setImage(bitmap);
				}
			}

			public void onFailed() {
				// TODO Auto-generated method stub
				showImgFaile(true);
			}
		});
	}
	private void showImgFaile(boolean b)
	{
		if(b)
		{
			iv_gif.setVisibility(View.GONE);
			img_down_fail_iv.setVisibility(View.VISIBLE);
		}
		else
		{
			iv_gif.setVisibility(View.VISIBLE);
			img_down_fail_iv.setVisibility(View.GONE);
		}
	}
	private void showLoading(boolean b) {
		if (b) {
			pb.setVisibility(View.VISIBLE);
			iv_gif.setVisibility(View.GONE);
		} else {
			pb.setVisibility(View.GONE);
			iv_gif.setVisibility(View.VISIBLE);
		}
	}


	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == zoom_in) {
			iv_gif.zoomIn();
		} else if (v == zoom_out) {
			iv_gif.zoomOut();
		} else if (v == image_save) {
			if (bitmap == null)
				return;
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				try {
					String fileName = imageurl.substring(imageurl.lastIndexOf("/") + 1, imageurl.lastIndexOf("."));
					if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
						String path = "/sdcard/" + AppConfig.SD_CARD_FOLDER + "/";
						File dir = new File(path);
						if (!dir.exists()) {
							dir.mkdirs();
						}
						File imageFile = new File(dir, fileName + ".jpeg");
						imageFile.createNewFile();
						FileOutputStream fos = new FileOutputStream(imageFile);
						bitmap.compress(CompressFormat.JPEG, 80, fos);
						fos.flush();
						fos.close();
						Toast.makeText(getApplicationContext(), "图片成功保存在：" + path + fileName + ".jpg",
								Toast.LENGTH_SHORT).show();
					}
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(),"保存图片失败",
							Toast.LENGTH_SHORT).show();
				}
			}
			else
			{
				Toast.makeText(getApplicationContext(),"保存图片失败:内存卡被挂起或不可用",
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			threadIsRuning = false;// 退出线程
		}
		return super.onKeyUp(keyCode, event);
	}

	public void actionDown() {

		long currentTime = System.currentTimeMillis();
		mTime = currentTime - lastTime;
		if (mTime < 4000 && button_layout.getVisibility() != View.GONE) {
			flag = false;
			lastTime = currentTime;
		} else {
			flag = true;
			button_layout.setVisibility(View.VISIBLE);
			if (thread == null || !threadIsRuning) {
				thread = new Thread(run);
				thread.start();
			}
			lastTime = currentTime;
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 1) {

				Animation alphaAnimation = new AlphaAnimation(1, 0);
				alphaAnimation.setDuration(1000);
				button_layout.startAnimation(alphaAnimation);
				button_layout.setVisibility(View.GONE);

			}
		}

	};

	Runnable run = new Runnable() {

		public void run() {
			while (threadIsRuning) {
				if (flag) {
					flag = false;
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Message msg = new Message();
					msg.what = 1;
					handler.sendEmptyMessage(msg.what);
				} else {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	};

}
